﻿using System.Windows.Forms;
using xfsale.domain;

namespace xfsale.lib
{
    public class ValueTextPair
    {
        private string _value;
        private string _text;

        public string Value
        {
            get { return this._value; }
            set { this._value = value; }
        }

        public string Text
        {
            get { return this._text; }
            set { this._text = value; }
        }
    }

    public static class SysMessage
    {
        public static void Info(string fmt, params object[] args)
        {
            string msg = string.Format(fmt, args);
            MessageBox.Show(msg, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        public static void Error(string fmt, params object[] args)
        {
            string msg = string.Format(fmt, args);
            MessageBox.Show(msg, "系统错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        public static void Warn(string fmt, params object[] args)
        {
            string msg = string.Format(fmt, args);
            MessageBox.Show(msg, "系统警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
    }

    public static class SysControl
    {
        public static void InitCtrl(Control ctrl, string dictName)
        {
            if (string.IsNullOrEmpty(dictName))
                return;

            dictName = dictName.ToUpper();
            if (!App.DictItems.ContainsKey(dictName))
                return;

            SysDictItem dictItem = App.DictItems[dictName] as SysDictItem;

            if (ctrl is ComboBox)
            {
                ComboBox cmb = ctrl as ComboBox;
                cmb.Sorted = true;
                cmb.Items.Clear();
                cmb.DisplayMember = "Text";
                cmb.ValueMember = "Value";
                foreach (SysDictValue item in dictItem.valueItems.Values)
                {
                    ValueTextPair pair = new ValueTextPair();
                    pair.Value = item.itemValue;
                    pair.Text = item.itemValue + " " + item.valueDesc;
                    cmb.Items.Add(pair);
                }
            }

            SetCtrlValue(ctrl, dictItem.itemDefault);
        }

        public static void ResetCtrl(Control ctrl, string dictName)
        {
            if (!ctrl.Enabled)
                return;

            string value = string.Empty;
            if (!string.IsNullOrEmpty(dictName))
            {
                dictName = dictName.ToUpper();
                if (App.DictItems.ContainsKey(dictName))
                {
                    SysDictItem dictItem = App.DictItems[dictName] as SysDictItem;
                    value = dictItem.itemDefault;
                }
            }

            SetCtrlValue(ctrl, value);
        }

        public static string GetCtrlValue(Control ctrl)
        {
            string value = string.Empty;
            if (ctrl is TextBox)
            {
                TextBox txt = ctrl as TextBox;
                value = txt.Text;
            }
            else if (ctrl is ComboBox)
            {
                ComboBox cmb = ctrl as ComboBox;
                if (cmb.SelectedItem is ValueTextPair)
                {
                    value = (cmb.SelectedItem as ValueTextPair).Value;
                }
            }
            return value;
        }

        public static void SetCtrlValue(Control ctrl, string value)
        {
            if (ctrl is TextBox)
            {
                TextBox txt = ctrl as TextBox;
                txt.Text = value;
            }
            else if (ctrl is ComboBox)
            {
                ComboBox cmb = ctrl as ComboBox;
                foreach (object item in cmb.Items)
                {
                    if (item is ValueTextPair)
                    {
                        if ((item as ValueTextPair).Value == value)
                        {
                            cmb.SelectedItem = item;
                            break;
                        }
                    }
                }
            }
        }
    }
}
